import axios from 'axios'
import { Message, Spin, Modal } from 'view-design';
import router from '../router/index.js';

const service = axios.create({
    timeout: 100000, // 请求超时时间
    baseURL: 'http://localhost:88/api', // API
});

// axios.defaults.baseURL = 'http://localhost:88/api';

service.interceptors.request.use(config=>{
    config.headers['accessToken'] = localStorage.getItem("accessToken");
    config.headers['refreshToken'] = localStorage.getItem("refreshToken");
    return config
})

service.interceptors.response.use(resp=>{

  if(resp.data.code == 416){
    // 如果返回码是416 说明用户的Token解码异常 让他去登陆
    localStorage.removeItem("accessToken")
    localStorage.removeItem("refreshToken")
    localStorage.removeItem("userInfo")

    Modal.confirm({
      title: '请登录',
      content: '<p>请登录后执行此操作</p>',
      okText: '重新登陆',
      cancelText: '继续浏览',
      onOk: () => {
        router.push({
          path: '/login',
          query: {
            rePath: router.history.current.path,
            query: JSON.stringify(router.history.current.query)
          }
        });
      },
      onCancel: () => {
        // router.go(0)
        router.push("/");
        Modal.remove();
      }
    });
  }

  if(resp.data.code == 415){
    // 返回码为415说明用户未登录
    localStorage.removeItem("accessToken")
    localStorage.removeItem("refreshToken")
    localStorage.removeItem("userInfo")

    Modal.confirm({
      title: '请登录',
      content: '<p>请登录后执行此操作</p>',
      okText: '重新登陆',
      cancelText: '继续浏览',
      onOk: () => {
        router.push({
          path: '/login',
          query: {
            rePath: router.history.current.path,
            query: JSON.stringify(router.history.current.query)
          }
        });
      },
      onCancel: () => {
        // router.go(0)
        router.push("/");
        Modal.remove();
      }
    });

  }

  if(resp.data.code == 413){
    // 如果返回码是413说明用户的荷载认证错误 把他的token移除 并重新登陆
    localStorage.removeItem("accessToken")
    localStorage.removeItem("refreshToken")
    localStorage.removeItem("userInfo")

    Modal.confirm({
      title: '用户信息错误',
      content: '<p>请重新登录后执行此操作</p>',
      okText: '重新登陆',
      cancelText: '继续浏览',
      onOk: () => {
        router.push({
          path: '/login',
          query: {
            rePath: router.history.current.path,
            query: JSON.stringify(router.history.current.query)
          }
        });
      },
      onCancel: () => {
        // router.go(0)
        router.push("/");
        Modal.remove();
      }
    });

  }

  if(resp.data.code == 411){
    // 如果返回码是411 说明当前用户持有的Token已过期需要重新登陆
    localStorage.removeItem("accessToken")
    localStorage.removeItem("refreshToken")
    localStorage.removeItem("userInfo")

    Modal.confirm({
      title: '登陆过期',
      content: '<p>请登录后执行此操作</p>',
      okText: '重新登陆',
      cancelText: '继续浏览',
      onOk: () => {
        router.push({
          path: '/login',
          query: {
            rePath: router.history.current.path,
            query: JSON.stringify(router.history.current.query)
          }
        });
      },
      onCancel: () => {
        // router.go(0)
        router.push("/");
        Modal.remove();
      }
    });

  }

  // 先不开启
  // if(resp.data.code == 0){
  //   // Token的其他异常 多为网络调用失败或者格式错误 用来防御使用postman等请求工具的攻击
  //   Message.error("网络异常,请稍后尝试")
  // }

  // 续签操作
  if(JSON.stringify(resp.headers.accesstoken)){
    localStorage.setItem("accessToken",resp.headers.accesstoken)
    localStorage.setItem("refreshToken",resp.headers.refreshtoken)
  }

  return resp
})


export const Method = {
    GET: 'get',
    POST: 'post',
    PUT: 'put',
    DELETE: 'delete'
  };

  export default function request (options) {
    return service(options);
  }  